package com.hfkh.keyun.controller.backend;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hfkh.keyun.common.ApiResult;
import com.hfkh.keyun.entity.Course;
import com.hfkh.keyun.entity.Evaluate;
import com.hfkh.keyun.entity.User;
import com.hfkh.keyun.mapper.CourseMapper;
import com.hfkh.keyun.mapper.UserMapper;
import com.hfkh.keyun.service.EvaluateService;
import com.hfkh.keyun.vo.EvaluateVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/api/evaluate")
@Api(tags = "评价管理")
public class EvaluateController {
    @Resource
    private UserMapper userMapper;
    @Resource
    private CourseMapper courseMapper;
    @Resource
    private EvaluateService evaluateService;
    /**
     * 查询所有评论
     * @return
     */
    @ApiOperation("查询全部评论")
    @GetMapping("/selectAll")
    public List<Evaluate> queryEvaluate(){
        List<Evaluate> evaluate = evaluateService.list(null);
        evaluate.forEach(System.out::println);
        return evaluate;
    }
    /**
     * 根据id删除评论
     * @param id
     */
    @ApiOperation("根据id删除评论")
    @DeleteMapping("/deleteEvaluate")
    public void deleteEvaluateById(Integer id){
        evaluateService.removeById(id);
    }
    /**
     * 查询评论(按照评论id)
     */
    @ApiOperation("按照评论id查询评论")
    @GetMapping("/queryOneEvaluate")
    public void getOneEvaluate(@RequestParam("id") Integer id){
        QueryWrapper<Evaluate> wrapper =new QueryWrapper<>();
        wrapper.eq("id",id);
        Evaluate evaluate = evaluateService.getOne(wrapper);
        User user = userMapper.selectById(evaluate.getUserId());
        evaluate.setUser(user);
        Course course = courseMapper.selectById(evaluate.getCourseId());
        evaluate.setCourse(course);
        System.out.println("用户对象"+user);
        System.out.println("课程对象"+course);
        System.out.println("评论内容："+evaluate.getContent());
    }
    /**
     *根据评论id分页查询
     */
    @ApiOperation("根据评论id分页查询")
    @GetMapping("/queryPage")
    @ApiImplicitParams(
            value = {
                    @ApiImplicitParam(name = "id",value = "编号",required = false,paramType = "query",dataType = "Integer",defaultValue = ""),
                    @ApiImplicitParam(name = "page",value = "页码",required = true,paramType = "query",dataType = "Integer",defaultValue = "1"),
                    @ApiImplicitParam(name = "size",value = "页容量",required = true,paramType = "query",dataType = "Integer",defaultValue = "10")
            }
    )
    public ApiResult queryPage(
            @RequestParam(required = false,defaultValue = "") Integer id, @RequestParam(defaultValue = "1")Integer page,
            @RequestParam(defaultValue = "10") Integer size){
        return evaluateService.queryPage(id,page,size);
    }
    @ApiOperation("修改评论信息")
    @PutMapping ("/updateEvaluateInfo")
    public ApiResult updateEvaluate(Integer id, EvaluateVO evaluateVO)  {
        Evaluate evaluate = new Evaluate();
        BeanUtils.copyProperties(evaluateVO,evaluate);
        evaluate.setId(id);
        evaluateService.updateById(evaluate);
        return ApiResult.success("修改评论信息");
    }
}
